//
//  WJVictorView.swift
//  WJWeiBo
//
//  Created by wangjun on 16/7/19.
//  Copyright © 2016年 wangjun. All rights reserved.
//

import UIKit
import SnapKit

class WJVictorView: UIView {
    
    
    //定义一个闭包
    var closure: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func loginBtn() {
        
        closure?()
    }
    
    //MARK: -向外界提供方法
    func setVisitorInfo(imgName: String?,title: String?) {
        
        if let img = imgName , let titleName = title {
            
            houseImg.image = UIImage(named: img)
            messageLabel.text = titleName
            
            feedImg.hidden = true
        }else{
            
            self.setAnimation()
        }
        
    }
    
    func setAnimation() {
        
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.duration = 10
        ani.toValue = M_PI * 2
        ani.repeatCount = MAXFLOAT
        
        feedImg.layer.addAnimation(ani, forKey: nil)
    }
    
    //MARK: -刷新界面,添加约束
    func setupUI() {
        backgroundColor = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        addSubview(feedImg)
        addSubview(maskImg)
        addSubview(houseImg)
        addSubview(messageLabel)
        addSubview(login)
        addSubview(regButton)
        
        feedImg.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        maskImg.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        houseImg.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(maskImg.snp_bottom).offset(15)
            make.centerX.equalTo(self)
            make.height.equalTo(36)
            make.width.equalTo(236)
        }
        
        login.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.leading.equalTo(messageLabel.snp_leading)
            make.height.equalTo(36)
            make.width.equalTo(100)
            
        }
        
        regButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(login.snp_top)
            make.height.equalTo(36)
            make.width.equalTo(100)
        }
        
    }
    
//    func text() {
//        
//        regButton.translatesAutoresizingMaskIntoConstraints = false
//        
//        
//        addConstraint(NSLayoutConstraint(item: regButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: <#T##AnyObject?#>, attribute: <#T##NSLayoutAttribute#>, multiplier: <#T##CGFloat#>, constant: <#T##CGFloat#>))
//    }
    
    /// 懒加载控件
    lazy var feedImg: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var maskImg = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var houseImg: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = .Center
        label.numberOfLines = 0
        return label
    }()
    
    lazy var login: UIButton = {
        let button = UIButton()
        
        button.addTarget(self, action: "loginBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        let image = UIImage(imageLiteral: "common_button_white_disable")
        
       let inImage = image.resizableImageWithCapInsets(UIEdgeInsetsMake(4, 4, 4, 4), resizingMode: UIImageResizingMode.Stretch)
        
        button.setBackgroundImage(inImage, forState: UIControlState.Normal)
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(20)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        return button
    }()
    
    lazy var regButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "loginBtn", forControlEvents: UIControlEvents.TouchUpInside)
        let image1 = UIImage(imageLiteral: "common_button_white_disable")
        
        let inImage1 = image1.resizableImageWithCapInsets(UIEdgeInsetsMake(4, 4, 4, 4), resizingMode: UIImageResizingMode.Stretch)
        button.setBackgroundImage(inImage1, forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(20)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        return button
    }()
    
    
    
    
}
